package com.xyzq.hs;

import com.xyzq.hs.util.ByteUtils;


public class RequestEntry {
	private static short DEFAULT_SYSTEMNO=2;
	private short systemNo;
	private int functionNo;
	private String params;
	private byte[] end=new byte[]{'\0'};
	
	
	public RequestEntry(int functionNo, String params) {
		super();
		this.systemNo=DEFAULT_SYSTEMNO;
		this.functionNo = functionNo;
		this.params = params;
	}

	/**
	 * entry=[systemNo,functionNo,paramsLen,params]
	 * @return
	 */
	public  byte[] asBytes(){
		byte[] systemNo=ByteUtils.short2byte(this.systemNo);
		byte[] functionNo=ByteUtils.int2byte(this.functionNo);
		if(this.params==null){
//			byte[] params=this.params.getBytes();
			byte[] paramsLen=new byte[]{0,1};
			return ByteUtils.merge(systemNo,functionNo,paramsLen);
		}else{
			byte[] params=this.params.getBytes();
			byte[] paramsLen=ByteUtils.short2byte((short)(params.length+1));
			return ByteUtils.merge(systemNo,functionNo,paramsLen,params,end);
		}
	}

	@Override
	public String toString() {
		return "RequestEntry [\n systemNo=" + systemNo + ",\n functionNo="
				+ functionNo + ",\n params=" + params + "]";
	}
	
	

}
